package cn.wolfcode.crm.service.imp;

import cn.wolfcode.crm.domain.*;
import cn.wolfcode.crm.mapper.InWarehouseItemMapper;
import cn.wolfcode.crm.mapper.InWarehouseMapper;
import cn.wolfcode.crm.mapper.ProductStockMapper;
import cn.wolfcode.crm.query.InWarehouseQueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IInWarehouseService;
import cn.wolfcode.crm.service.IProductStockService;
import cn.wolfcode.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class InWarehouseServiceImpl implements IInWarehouseService {
    @Autowired
    private InWarehouseMapper inWarehouseMapper;
    @Autowired
    private InWarehouseItemMapper inWarehouseItemMapper;
    @Autowired
    private ProductStockMapper productStockMapper;

    public InWarehouse selectByPrimaryKey(Long id) {
        return inWarehouseMapper.selectByPrimaryKey(id);
    }

    public List<InWarehouse> selectAll() {
        return inWarehouseMapper.selectAll();
    }


    public PageResult query(InWarehouseQueryObject qo) {
        int total = inWarehouseMapper.selectCount(qo);
        if(total==0){
            return new PageResult(total, Collections.EMPTY_LIST);
        }
        List<InWarehouse> rows = inWarehouseMapper.queryForList(qo);
        return new PageResult(total,rows);
    }

    public void saveOrUpdate(InWarehouse order) {
        if (order.getId() == null) {
            save(order);
        }else {
            update(order);
        }

    }


    //新增订单
    private void save(InWarehouse order) {
        order.setInputUser(UserContext.getCurrentUser());
        order.setInputTime(new Date());
        order.setStatus(InWarehouse.NORMAL);
        BigDecimal totaNumber = BigDecimal.ZERO;
        BigDecimal totalAmount = BigDecimal.ZERO;
        //保存订单的明细
        for (InWarehouseItem item : order.getItems()) {
            BigDecimal costPrice = item.getCostPrice();
            BigDecimal number = item.getNumber();
            BigDecimal amout = costPrice.multiply(number).setScale(2, RoundingMode.HALF_UP);
            item.setAmount(amout);
            totalAmount = totalAmount.add(amout);
            totaNumber = totaNumber.add(number);
        }
        order.setTotalAmount(totalAmount);
        order.setTotalNumber(totaNumber);
        //保存单据
        inWarehouseMapper.insert(order);
        //保存明细
        for (InWarehouseItem item : order.getItems()) {
            item.setBillId(order.getId());
            inWarehouseItemMapper.insert(item);
        }
    }
    //编辑订单
    private void update(InWarehouse order) {

    }

    //修改状态
    @Override
    public void audit(Long id) {
        InWarehouse bill = inWarehouseMapper.selectByPrimaryKey(id);
        if (bill.getStatus() == InWarehouse.NORMAL){
            Depot depot = bill.getDepot();
            //明细中的商品
            for (InWarehouseItem item : bill.getItems()) {
                Product p = item.getProduct();
                ProductStock stock = productStockMapper.selectStockByprotIdAndDepotId(p.getId(), depot.getId());
                //如果库存中已经存在商品则更改
                if (stock!=null){
                    BigDecimal amount = item.getAmount().add(stock.getAmount());
                    BigDecimal number = item.getNumber().add(stock.getStoreNumber());
                    BigDecimal price = amount.divide(number, 2, RoundingMode.HALF_UP);
                    stock.setAmount(amount);
                    stock.setStoreNumber(number);
                    stock.setPrice(price);
                    productStockMapper.updateByPrimaryKey(stock);
                    //如果库存不存在商品就直接添加
                }else {
                    stock = new ProductStock();
                    stock.setPrice(item.getCostPrice());
                    stock.setStoreNumber(item.getNumber());
                    stock.setAmount(item.getAmount());
                    stock.setDepot(depot);
                    stock.setProduct(p);
                    productStockMapper.insert(stock);
                }
            }
            bill.setAuditor(UserContext.getCurrentUser());
            bill.setAuditTime(new Date());
            bill.setStatus(InWarehouse.AUDIT);
            inWarehouseMapper.audit(bill);
        }
    }
}
